package com.bookmanager.www.common.utils;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: 风离
 * @Date: 2021/09/23/15:04
 * @Description:
 */
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class TestHttpClient {
    public static void main(String[] args) throws IOException {
        String urlParame="https://api.zuk.pw/situ/book/isbn/9787530212004";
        System.out.println(sendGet(urlParame));
        String json = sendGet(urlParame);
        JSONObject obj = JSON.parseObject(json);
        System.out.println(obj.getString("ret"));
        JSONObject obj2 =JSON.parseObject(obj.getString("data"));
        System.out.println(obj2.getString("description"));
    }





    public static String sendGet(String urlParame) throws IOException {
        //创建httpclient实例
        HttpClient httpClient=new HttpClient();
        //设置httpclient连接主机服务器超时时间： 以毫秒为单位 1000ms=1s
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
        //创建get请求方法实例对象
        GetMethod getMethod=new GetMethod(urlParame);
        //设置get请求超时时间，value以毫秒为单位
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT,10000);
        //设置请求头
        getMethod.addRequestHeader("Content-Type", "application/json");
        //执行get
        httpClient.executeMethod(getMethod);
        //获取返回数据
        String result=getMethod.getResponseBodyAsString();
        //释放http连接
        getMethod.releaseConnection();

        return result;
    }


    public static List getPersonList(String jsonstring) {

        List list = new ArrayList();

        try {

            list = JSON.parseArray(jsonstring);
            

        } catch (Exception e) {

            // TODO: handle exception

        }

        return list;

    }
}

